package atguigu.reg;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo1 {
    public static void main(String[] args) {
        String regex = "^\\d{4}(-\\d{1,2}(-\\d{1,2})?)?$";
        String[] dates = {"2023-07-30", "2023-7-30", "2023"};
        for (String date : dates) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(date);
            if (matcher.matches()) {
                System.out.println("匹配成功: " + date);
            } else {
                System.out.println("匹配失败: " + date);
            }
        }
    }
}
